package com.ruoyi.fly.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.fly.domain.AppletProductOrder;
import com.ruoyi.fly.domain.Product;
import com.ruoyi.fly.domain.Store;
import com.ruoyi.fly.domain.Vehicles;
import com.ruoyi.fly.domain.vo.AppletProductOrderVo;
import com.ruoyi.fly.domain.vo.ProductOrderQueryVo;
import com.ruoyi.fly.domain.vo.ProductOrderVo;
import com.ruoyi.fly.mapper.AppletProductOrderMapper;
import com.ruoyi.fly.mapper.ProductMapper;
import com.ruoyi.fly.mapper.StoreMapper;
import com.ruoyi.fly.mapper.VehiclesMapper;
import com.ruoyi.fly.service.AppletProductOrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AppletProductOrderServiceImpl implements AppletProductOrderService {

    @Autowired
    private AppletProductOrderMapper appletProductOrderMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private VehiclesMapper vehiclesMapper;

    @Autowired
    private ProductMapper productMapper;

    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Override
    public ProductOrderVo selectAppletProductOrderById(String id) {
        ProductOrderVo productOrderVo = new ProductOrderVo();
        AppletProductOrder appletProductOrder = appletProductOrderMapper.selectAppletProductOrderById(id);
        Store store = storeMapper.selectStoreById(appletProductOrder.getStoreId());
        Vehicles vehicles = vehiclesMapper.selectPcVehiclesById(appletProductOrder.getVehiclesId());
        Product product = productMapper.selectProductById(appletProductOrder.getProductId());
        BeanUtils.copyProperties(appletProductOrder, productOrderVo);
        productOrderVo.setProductName(product.getProductName());
        productOrderVo.setStoreName(store.getStoreName());
        productOrderVo.setLicensePlate(vehicles.getLicensePlate());
        return productOrderVo;
    }

    /**
     * 查询订单列表
     * @param appletProductOrder
     * @return
     */
    @Override
    public List<ProductOrderVo> selectAppletProductOrderList(AppletProductOrder appletProductOrder) {
        List<AppletProductOrder> appletProductOrders = appletProductOrderMapper.selectAppletProductOrderList(appletProductOrder);
        List<ProductOrderVo> productOrderVos= new ArrayList<>();
        ProductOrderVo productOrderVo = new ProductOrderVo();
        appletProductOrders.forEach(appletProductOrder1 -> {
            Store store = storeMapper.selectStoreById(appletProductOrder1.getStoreId());
            Vehicles vehicles = vehiclesMapper.selectPcVehiclesById(appletProductOrder1.getVehiclesId());
            Product product = productMapper.selectProductById(appletProductOrder1.getProductId());
            BeanUtils.copyProperties(appletProductOrder1, productOrderVo);
            productOrderVo.setProductName(product.getProductName());
            productOrderVo.setStoreName(store.getStoreName());
            productOrderVo.setLicensePlate(vehicles.getLicensePlate());
            productOrderVos.add(productOrderVo);
        });
        return productOrderVos;
    }

    /**
     * 根据条件查询订单
     * @param productOrderQueryVo
     * @return
     */
    @Override
    public AppletProductOrder selectAppletProductOrder(ProductOrderQueryVo productOrderQueryVo) {
        return appletProductOrderMapper.selectAppletProductOrder(productOrderQueryVo);
    }

    /**
     * 添加快递单号
     * @param appletProductOrderVo
     * @return
     */
    @Override
    public R updateAppletProductOrder(AppletProductOrderVo appletProductOrderVo) {
        return appletProductOrderMapper.updateAppletProductOrder(appletProductOrderVo) > 0 ? R.ok("添加成功") : R.fail("删除失败");
    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @Override
    public R deleteAppletProductOrderById(String id) {
        return appletProductOrderMapper.deleteAppletProductOrderById(id) > 0 ? R.ok("删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteAppletProductOrderByIds(List<String> ids) {
        return appletProductOrderMapper.deleteAppletProductOrderByIds(ids) > 0 ? R.ok("删除成功") : R.fail("删除失败");
    }
}
